<?php
/**
 * Created by PhpStorm.
 * User: lili
 * Date: 2018/11/23
 * Time: 16:02
 */

namespace app\admin\model;


use app\index\model\Beginningtreatment;
use app\index\model\Item;
use app\index\model\Reason;
use think\Model;
use traits\model\SoftDelete;

class Check extends Model
{


    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $autoWriteTimestamp = true;


    /*
     * 记录血糖数组
     * @param int $page 页码
     * @param int $limit 页内显示
     * @return array 血糖记录集合
     *
     *
     * */
    public function getBloodSugarCheck($page=1,$limit=10,$w=''){
        
        
        
        
        $w2['itemid'] = ['like','1%'];
        
        if(!empty($w['date'])){
            $w3['date'] = $w['date'];
        }

        if(!empty($w['itemid'])){
            $w3['itemid'] = $w['itemid'];
        }
        if(!empty($w['type'])){
            $w3['type'] = $w['type'];
        }
        if(!empty($w['health'])){
            $w3['health'] = $w['health'];
        }
        if(!empty($w['username'])){
            $w3['username'] = $w['username'];

        }

//        dump($w3);
        $start = ($page-1)*$limit;

        if(empty($w3)){
            $bscheck = $this
                ->alias('c')
                ->join('__USER__ u ','c.uid = u.uid')
                ->where($w2)
                ->limit($start,$limit)
                ->select();
        }else{
            $bscheck = $this
                ->alias('c')
                ->join('__USER__ u ','c.uid = u.uid')
                ->where($w2)
                ->where($w3)
                ->limit($start,$limit)
                ->select();
//            dump($this->getLastSql());
        }


        if (!empty($bscheck)){
            foreach ($bscheck as $i=>$v){
                $itemidsarr = explode(',',$v['itemid'] );
                $itemid = $itemidsarr[1];
                $itemmodel = new Item();
                $item = $itemmodel->where('itemid',$itemid)->find();
                $bscheck[$i]['itemname'] = $item['itemname'];//项目名称
//                dump($v['health']);
                switch ($v['health']){
                    case 1:$bscheck[$i]['health']='健康';break;
                    case 2:$bscheck[$i]['health']='低血糖';break;
                    case 3:$bscheck[$i]['health']='超低血糖';break;
                    case 4:$bscheck[$i]['health']='高血糖';break;
                    case 5:$bscheck[$i]['health']='超高血糖';break;
                    default : $bscheck[$i]['health']='健康';
                }

                switch ($v['type']){
                    case 1:$bscheck[$i]['type']='低血糖';break;
                    case 2:$bscheck[$i]['type']='高血糖';break;
                    case 3:$bscheck[$i]['type']='正常';break;
                    default : $bscheck[$i]['type']='正常';
                }

                /*$usermodel = new User();
                $user = $usermodel->getUserByuid($v['uid']);
                $bscheck[$i]['username']=$user['username'];*/


            }
            return $bscheck;

        }else{
            return null;
        }

    }
    
    
    /*
     * 获得血糖记录
     * @param int $cid 监测表id
     * 
     * 
     * */
    public function getBloodCheck($cid){
        $w['cid'] = $cid;
        $bc = $this->where($w)->find();
        $itemidsarr = explode(',',$bc['itemid'] );
        $itemid = $itemidsarr[1];
        $itemmodel = new Item();
        $item = $itemmodel->where('itemid',$itemid)->find();
        $bc['itemname'] = $item['itemname'];//项目名称
        switch ($bc['health']){
            case 1:$bc['health']='健康';break;
            case 2:$bc['health']='低血糖';break;
            case 3:$bc['health']='超低血糖';break;
            case 4:$bc['health']='高血糖';break;
            case 5:$bc['health']='超高血糖';break;
            default : $bc['health']='健康';
        }
        
        switch ($bc['type']){
            case 1:$bc['type']='低血糖';break;
            case 2:$bc['type']='高血糖';break;
            case 3:$bc['type']='正常';break;
            default : $bc['type']='正常';
        }
        $usermodel = new User();
        $user = $usermodel->getUserByuid($bc['uid']);
        $bc['username']=$user['username'];

        return $bc;
    }



    /*
     * 餐前大剂量列表
     * @param int $uid 用户id
     * @return array 餐前大剂量
     *
     *
     * */
    public function getbeforeeatingChecklist($page=1,$limit=10,$w){

        $w2['itemid'] = ['like','2%'];

        if(!empty($w['date'])){
            $w3['date'] = $w['date'];
        }

        if(!empty($w['itemid'])){
            $w3['itemid'] = $w['itemid'];
        }

        if(!empty($w['username'])){
            $w3['username'] = $w['username'];

        }


        $start = ($page-1)*$limit;
        if(empty($w3)){
            $check = $this
                ->alias('c')
                ->join('__USER__ u ','c.uid = u.uid')
                ->where($w2)
                ->limit($start,$limit)
                ->select();
        }else{
            $check = $this
                ->alias('c')
                ->join('__USER__ u ','c.uid = u.uid')
                ->where($w2)
                ->where($w3)
                ->limit($start,$limit)
                ->select();
//            dump($this->getLastSql());
        }


        if (!empty($check)){
            foreach ($check as $i=>$v){
                $itemidsarr = explode(',',$v['itemid'] );
                $itemid = $itemidsarr[1];
                $itemmodel = new Item();
                $item = $itemmodel->where('itemid',$itemid)->find();
                $check[$i]['itemname'] = $item['itemname'];//项目名称
//                dump($v['health']);
                 

              /*  $usermodel = new User();
                $user = $usermodel->getUserByuid($v['uid']);
                $check[$i]['username']=$user['username'];*/


            }
            return $check;

        }else{
            return null;
        }

    }


    /*
     * 获得餐前大剂量详细信息
     *
     *
     * */
    public function getbeforeeatingCheck($cid){
        $w['cid'] = $cid;
        $bc = $this->where($w)->find();
        $itemidsarr = explode(',',$bc['itemid'] );
        $itemid = $itemidsarr[1];
        $itemmodel = new Item();
        $item = $itemmodel->where('itemid',$itemid)->find();
        $bc['itemname'] = $item['itemname'];//项目名称
        $usermodel = new User();
        $user = $usermodel->getUserByuid($bc['uid']);
        $bc['username']=$user['username'];
        return $bc;
    }


    /*
     * 临时大剂量列表
     * @param int $uid 用户id
     * @return array 餐前大剂量
     *
     *
     * */
    public function gettemporaryChecklist($page=1,$limit=10,$w){

        $w2['itemid'] = ['like','2%'];

        if(!empty($w['date'])){
            $w3['date'] = $w['date'];
        }



        if(!empty($w['username'])){
            $w3['username'] = $w['username'];

        }


        $start = ($page-1)*$limit;

        if(empty($w3)){
            $check = $this
                ->alias('c')
                ->join('__USER__ u ','c.uid = u.uid')
                ->where($w2)
                ->limit($start,$limit)
                ->select();
        }else{
            $check = $this
                ->alias('c')
                ->join('__USER__ u ','c.uid = u.uid')
                ->where($w2)
                ->where($w3)
                ->limit($start,$limit)
                ->select();
//            dump($this->getLastSql());
        }

        if (!empty($check)){
            foreach ($check as $i=>$v){
                $itemidsarr = explode(',',$v['itemid'] );


//                dump($v['health']);
                if (!empty($itemidsarr[1])){
                    $itemid = $itemidsarr[1];
                    $itemmodel = new Item();
                    $item = $itemmodel->where('itemid',$itemid)->find();
                    $check[$i]['itemname'] = $item['itemname'];//项目名称
                }

              /*  $usermodel = new User();
                $user = $usermodel->getUserByuid($v['uid']);
                $check[$i]['username']=$user['username'];*/


            }
            return $check;

        }else{
            return null;
        }

    }


    /*
     * 获得餐前大剂量详细信息
     *
     *
     * */
    public function gettemporaryCheck($cid){
        $w['cid'] = $cid;
        $bc = $this->where($w)->find();
        $itemidsarr = explode(',',$bc['itemid'] );

        if (!empty($itemidsarr[1])){

            $itemid = $itemidsarr[1];
            $itemmodel = new Item();
            $item = $itemmodel->where('itemid',$itemid)->find();
            $bc['itemname'] = $item['itemname'];//项目名称
        }
        $usermodel = new User();
        $user = $usermodel->getUserByuid($bc['uid']);
        $bc['username']=$user['username'];
        return $bc;
    }



    /*
     * 获得Excel的数组
     * @param int $uid 用户id
     * @param string $date 时间戳
     *
     * */
    public function getDataExport($uid,$date){


        //如果数据库有，就直接返回
        $excelmodel = new Excel();
        $data = $excelmodel->findExcelData($uid, $date);
        if(!empty($data)){
            return $data;
        }


        $data = array();
        $usermodel = new User();
        $user = $usermodel->where('uid',$uid)->find();//个人信息
//        dump($uid);
        $data['B'] =  $user['uid'];
        $data['C'] =  $user['username'];
        $data['D'] =  date('Ymd',$date);
        $data['AX'] =  $user['sex']== 1 ? '男' : '女';
        $data['AY'] =  $user['age'];
        $data['AZ'] =  $user['height'];
        $data['BA'] =  $user['weight'];

        //初始方案
        $btmodel = new Beginningtreatment();
        $bt = $btmodel->getBTByUid($uid,$date);
        $checkmodel = new \app\index\model\Check();
       
        $sdate = date('Y-m-d',$date);//格式化时间
        $beforeeatinsulinDate = $checkmodel->getbeforeeatinsulinOfDate($uid,$sdate);//历史记录的餐前大剂量总量
        $temporaryinsulinDate = $checkmodel->gettemporaryinsulinOfDate($uid,$sdate);//历史记录的临时大剂量总量
//        dump($sdate);


        $data['E'] = $bt['insulin']==1?'速效胰岛素':'短效胰岛素';
        $data['G'] = $bt['baseinsulin'];//基础率总量
        $data['H'] = $beforeeatinsulinDate;//餐前总量
        $data['I'] = $temporaryinsulinDate;//临时大剂量总量
        $data['F'] = $data['G']+$data['H']+$data['I'];//总量
        $data['BC'] = $bt['hospital'];//就诊医院
        $data['BB'] = $bt['isotherdisease']==1?$bt['otherdisease']:null;//既往病史

        $data['AR'] = $bt['sulin0003'];//就诊医院
        $data['AS'] = $bt['sulin0307'];//就诊医院
        $data['AT'] = $bt['sulin0712'];//就诊医院
        $data['AU'] = $bt['sulin1217'];//就诊医院
        $data['AV'] = $bt['sulin1722'];//就诊医院
        $data['AW'] = $bt['sulin2224'];//就诊医院


        //血糖和胰岛素记录
        $bsl = $checkmodel->getBloodSugarCheckOfDate($uid, $sdate);//血糖列表
//        dump($bsl);
        $bfel = $checkmodel->getBeforeeatingListOfDate($uid, $sdate);//餐前大剂量列表
        $til = $checkmodel->gettemporaryinsulinListOfDate($uid, $sdate);//临时大剂量列表

        //血糖
        $data['J'] = strtotime($bsl['夜间3点']['date']) == null ? null : date('H:i',strtotime($bsl['夜间3点']['date']))   ;//时间
        $data['K'] = $bsl['夜间3点']['value'];//血糖值

        $data['L'] = strtotime($bsl['早餐前']['date']) == null ? null : date('H:i',strtotime($bsl['早餐前']['date']))   ;//时间
        $data['M'] = $bsl['早餐前']['value'];//血糖值

        $data['N'] = strtotime($bsl['早餐后']['date']) == null ? null : date('H:i',strtotime($bsl['早餐后']['date']))   ;//时间
        $data['O'] = $bsl['早餐后']['value'];//血糖值

        $data['P'] = strtotime($bsl['午餐前']['date']) == null ? null : date('H:i',strtotime($bsl['午餐前']['date']))   ;//时间
        $data['Q'] = $bsl['午餐前']['value'];//血糖值

        $data['R'] = strtotime($bsl['午餐后']['date']) == null ? null : date('H:i',strtotime($bsl['午餐后']['date']))   ;//时间
        $data['S'] = $bsl['午餐后']['value'];//血糖值

        $data['T'] = strtotime($bsl['晚餐前']['date']) == null ? null : date('H:i',strtotime($bsl['晚餐前']['date']))   ;//时间
        $data['U'] = $bsl['晚餐前']['value'];//血糖值

        $data['V'] = strtotime($bsl['晚餐后']['date']) == null ? null : date('H:i',strtotime($bsl['晚餐后']['date']))   ;//时间
        $data['W'] = $bsl['晚餐后']['value'];//血糖值

        $data['X'] = strtotime($bsl['睡前']['date']) == null ? null : date('H:i',strtotime($bsl['睡前']['date']))   ;//时间
        $data['Y'] = $bsl['睡前']['value'];//血糖值

        //餐前
        $data['Z'] = strtotime($bfel['早餐']['date']) == null ? null : date('H:i',strtotime($bfel['早餐']['date']))   ;//时间
        $data['AA'] = $bfel['早餐']['value'];//血糖值

        $data['AB'] = strtotime($bfel['午餐']['date']) == null ? null : date('H:i',strtotime($bfel['午餐']['date']))   ;//时间
        $data['AC'] = $bfel['午餐']['value'];//血糖值

        $data['AD'] = strtotime($bfel['晚餐']['date']) == null ? null : date('H:i',strtotime($bfel['晚餐']['date']))   ;//时间
        $data['AE'] = $bfel['晚餐']['value'];//血糖值


        //临时
        $data['AF'] = strtotime($til['临时剂量1']['date']) == null ? null : date('H:i',strtotime($til['临时剂量1']['date']))   ;//时间
        $data['AG'] = $til['临时剂量1']['value'];//血糖值

        $data['AH'] = strtotime($til['临时剂量2']['date']) == null ? null : date('H:i',strtotime($til['临时剂量2']['date']))   ;//时间
        $data['AI'] = $til['临时剂量2']['value'];//血糖值

        $data['AJ'] = strtotime($til['临时剂量3']['date']) == null ? null : date('H:i',strtotime($til['临时剂量3']['date']))   ;//时间
        $data['AK'] = $til['临时剂量3']['value'];//血糖值

        $data['AL'] = strtotime($til['临时剂量4']['date']) == null ? null : date('H:i',strtotime($til['临时剂量4']['date']))   ;//时间
        $data['AM'] = $til['临时剂量4']['value'];//血糖值

        $data['AN'] = strtotime($til['临时剂量5']['date']) == null ? null : date('H:i',strtotime($til['临时剂量5']['date']))   ;//时间
        $data['AO'] = $til['临时剂量5']['value'];//血糖值


        $data['AP'] = '';
        $data['AQ'] = '';
        foreach ($bsl as $item => $value){
            if(!empty(strtotime($value['date']))  ){
                switch ($value['type']){
                    case 1:{ //低血糖
                        if ( !empty($value['reasonid']) ){
//                            $rids = explode(',',$value['reasonid'] );
                            $reasonmodel = new Reason();
                            $reasonarr = collection($reasonmodel->where('rid','in',$value['reasonid'])->select())->toArray();
                            $reasonstr = '';

                            $reasonstr.= implode(',', array_column($reasonarr, 'reason')) ;

                            $data['AP'] = $item.':'.$reasonstr ;
                            if ( !empty($value['reason']) ){
                                $data['AP'] .= ':'.$value['reason'].';';
                            }else{
                                $data['AP'].= ';';
                            }

                             
                        }
                        break;
                    }
                    case 2:{ //高血糖
                        if ( !empty($value['reasonid']) ){
//                            $rids = explode(',',$value['reasonid'] );
                            $reasonmodel = new Reason();
                            $reasonarr = collection($reasonmodel->where('rid','in',$value['reasonid'])->select())->toArray();
                            $reasonstr = '';

                            $reasonstr.= implode(',', array_column($reasonarr, 'reason')) ;
                            $data['AQ'] = $item.':'.$reasonstr ;
                            if ( !empty($value['reason']) ){
                                $data['AQ'] .= ':'.$value['reason'].';';
                            }else{
                                $data['AQ'].= ';';
                            }
                        }
                        break;
                    }
                    default:;
                }
            }
        }



        $excelmodel = new Excel();
        $excelmodel->isUpdate(false)->save($data);




        return $data;


    }


    
}